package edu.ynu.se.xiecheng.achitectureclass.project.service;

import edu.ynu.se.xiecheng.achitectureclass.common.service.LogicService;
import edu.ynu.se.xiecheng.achitectureclass.project.dao.AirTemperatureDataDAO;
import edu.ynu.se.xiecheng.achitectureclass.project.dao.SoilPhosphorusDataDAO;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.Greenhouse;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.SoilPhosphorusData;
import edu.ynu.se.xiecheng.achitectureclass.project.utils.GreenhouseDeviceUtils;
import edu.ynu.se.xiecheng.achitectureclass.project.utils.SensorApiClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.HttpCookie;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SoilPhosphorusDataService extends LogicService<SoilPhosphorusDataDAO, SoilPhosphorusData, Long> {
    @Autowired
    private SensorApiClient sensorApiClient;

    @Autowired
    private GreenhouseDeviceUtils greenhouseDeviceUtils;


    public SoilPhosphorusDataService(SoilPhosphorusDataDAO lr) {
        super(lr);
    }

    /**
     * 从传感器API获取含磷量数据并存储到数据库
     */
    public void fetchAndSavePhosphorusData() {
        // 示例 Box 编号和传感器 ID 列表
        String boxNo = "300822040454";
        List<String> sensorIds = List.of("327061586065758009");

        // 调用外部接口获取传感器数据
        List<Map<String, Object>> sensorData = sensorApiClient.fetchSensorData(sensorIds, boxNo);

        for (Map<String, Object> data : sensorData) {
            System.out.println("Sensor data: " + data);

            // 创建或更新大棚信息
            String boxId = data.get("boxId").toString(); // 大棚 ID
            Greenhouse greenhouse = greenhouseDeviceUtils.getOrCreateGreenhouse(boxId);

            // 创建或更新设备信息
            String sensorId = data.get("id").toString(); // 传感器设备 ID
            String name = data.get("name").toString();
            Device device = greenhouseDeviceUtils.getOrCreateDevice(sensorId, name, greenhouse);

            //创建含磷量数据实例并设置属性
            SoilPhosphorusData soilPhosphorusData = new SoilPhosphorusData();
            soilPhosphorusData.setName(name);
            soilPhosphorusData.setValue(Double.valueOf(data.get("value").toString()));
            soilPhosphorusData.setUnit("mg/kg");
            soilPhosphorusData.setTimestamp(new Date());
            soilPhosphorusData.setDevice(device);

            dao.save(soilPhosphorusData);
        }
    }

    /**
     * 获取最新的含磷量数据
     * @return
     */
    public SoilPhosphorusData getLatestPhosphorusData() {
        SoilPhosphorusData soilPhosphorusData = dao.findFirstByOrderByTimestampDesc();
        if (soilPhosphorusData != null){
            return soilPhosphorusData;
        }else
            return new SoilPhosphorusData();
    }

    /**
     * 评估含磷量数据的状态
     * @param value
     * @return
     */
    public String evaluateStatus(Double value) {
        if (value == null) return "未知";
        if (value >= 10) return "优";
        if (value >= 0) return "良";
        return "差";
    }
}
